﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.PartyRepresentative
{
    public partial class MyPartyRepresentativeMeetingWithEdit : PageBase
    {
        #region 属性
        
        /// <summary>
        /// 工作室ID
        /// </summary>
        public string StudioID
        {
            get
            {
                return GetQueryIntValue("StudioID"); ;
            }
        }
        #endregion
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get { return ""; }
        }

        #endregion

        #region 页面属性

        public string EditorContent { get; set; }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            //工作室加载
            ControlBusiness.ControlBind(
                ddlStudio, DB.PartyRepresentativeStudios.OrderBy(a => a.CreateTime),
                "StudioName", "ID");
            ddlStudio.SelectedValue = StudioID;
            //党代表加载 
            ControlBusiness.ControlBind(ddlRepresentativeID, DB.PartyRepresentativeInfos.OrderBy(a => a.OrderNo), "UserName", "ID");
            BindMeeting();
           
              
            
        }

        public void BindMeeting()
        {
            var meetings = DB.PartyRepresentativeMeetings.Where(a => a.BeginTime > DateTime.Now && a.StudioID == StudioID);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("Title", typeof(string));
            if (!meetings.Any())
            {
                //var dr = dt.NewRow();
                //dr["ID"] = "";
                //dr["Title"] = "";
                //dt.Rows.Add(dr);
                labTip.Hidden = false;
            }
            else
            {
                labTip.Hidden = true;
                foreach (var item in meetings)
                {
                    var dr = dt.NewRow();
                    dr["ID"] = item.ID;
                    dr["Title"] = string.Format("{0}{1}【日期：{2}】", item.Title,
                        PartyRepresentativeInfo.GetPartyRepresentativeJoin(DB, item.ID).Replace("【", "【党代表："), Convert.ToDateTime(item.BeginTime).ToString("yyyy-MM-dd HH:mm"));
                    dt.Rows.Add(dr);
                }
            }
            ControlBusiness.ControlBind(ddlMeeting, ds, "Title", "ID");
        }

        private void LoadData()
        {
            if (string.IsNullOrEmpty(StudioID))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());

                return;
            }
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                var current = DB.PartyRepresentativeMeetingWiths.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    //处理待办
                    PartyTaskHelper.TaskCancel(id, CurrentUser.LoginID);
                    return;
                }
                txtTitle.Text = current.Title;
                txtUserName.Text = ControlBusiness.GetMembersPickerText(current.WithBy);
                txtMobileNo.Text = current.MobileNo;
                txtPost.Text = current.Post;
                ddlRepresentativeID.SelectedValue = current.RepresentativeID;
                txtWorkUnit.Text = current.WorkUnit;
                txtContent.Text = current.Content;
                this.ddlStatus.SelectedValue = current.Status.ToString();


                this.ddlMeeting.Readonly = true;
                this.ddlStatus.Readonly = true;
               
                this.ddlMeeting.SelectedValue = current.MeetingID;
            }

        }

        #endregion

        #region Events

        private void SaveData()
        {
            
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();


        }


        #endregion

    }
}